package net.awpspace.matheasy;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;

public class MenuScreen extends GameCanvas {

	MathEasy main;
	Image background, immenu, implaydown, implayup, imhelpdown, imhelpup,
			imaboutdown, imaboutup, imexitdown, imexitup, panel;
	Button play, help, about, exit;
	MySprite menu;
	boolean isIn = false;
	Command back;
	public MenuScreen(MathEasy main) {
		super(true);
		this.setFullScreenMode(true);
		this.main = main;
		loadContent();
		setListener();
		back = new Command("back", Command.BACK, 1);
		addCommand(back);
		setCommandListener(new CommandListener() {
			
			public void commandAction(Command c, Displayable d) {
				if(c.getLabel().equals("back")){
					MenuScreen.this.main.exitMIDlet();
				}
			}
		});
	}

	private void setListener() {
		play.setListener(new ButtonListener() {
			public void actionUp() {
				super.actionUp();
				main.display.setCurrent(new GameScreen(main));
			}
		});
		help.setListener(new ButtonListener() {
			public void actionUp() {
				super.actionUp();
				main.display.setCurrent(new HelpScreen(main));
			}
		});
		about.setListener(new ButtonListener() {
			public void actionUp() {
				super.actionUp();
				showAbout();
			}
		});
		exit.setListener(new ButtonListener() {
			public void actionUp() {
				super.actionUp();
				main.more();
			}
		});
	}

	public void showAbout() {
		Alert dialog = new Alert("About", "Development by withlovei."
				+ "\nhttps://www.facebook.com/AwpSpace?ref=hl"
				+ "\nEmail:phamkyit@gmail.com"
				+ "\n@Copyright of Awpspace team", null, AlertType.INFO);
		dialog.addCommand(new Command("ok", Command.OK, 1));
		dialog.setCommandListener(new CommandListener() {

			public void commandAction(Command c, Displayable d) {
				main.display.setCurrent(MenuScreen.this);
			}
		});
		main.display.setCurrent(dialog, this);
	}

	private void loadContent() {
		background = ImageLoader.loadImage("/bgmenu.png");
		imaboutdown = ImageLoader.loadImage("/aboutdown.png");
		imaboutup = ImageLoader.loadImage("/aboutup.png");
		immenu = ImageLoader.loadImage("/menu.png");
		imhelpdown = ImageLoader.loadImage("/helpdown.png");
		imhelpup = ImageLoader.loadImage("/helpup.png");
		implaydown = ImageLoader.loadImage("/playdown.png");
		implayup = ImageLoader.loadImage("/playup.png");
		imexitdown = ImageLoader.loadImage("/exitmenudown.png");
		imexitup = ImageLoader.loadImage("/exitmenuup.png");
		panel = ImageLoader.loadImage("/panelmenu.png");
		menu = new MySprite(immenu, 2, 1, 193, 279);
		play = new Button(implayup, implaydown, 135, 136);
		help = new Button(imhelpup, imhelpdown, 133, 173);
		about = new Button(imaboutup, imaboutdown, 121, 209);
		exit = new Button(imexitup, imexitdown, 132, 245);
	}

	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);
		g.fillRect(0, 0, 240, 320);
		g.drawImage(background, 0, 0, 0);
		if (isIn) {
			g.drawImage(panel, 103, 127, 0);
			play.draw(g);
			help.draw(g);
			about.draw(g);
			exit.draw(g);
		}
		menu.paint(g);

	}

	protected void pointerPressed(int x, int y) {
		super.pointerPressed(x, y);
		if (isIn) {
			play.actionDown(x, y);
			help.actionDown(x, y);
			about.actionDown(x, y);
			exit.actionDown(x, y);
		}
		if (inSprite(menu, x, y)) {
			isIn = !isIn;
			menu.nextFrame();
		}
		repaint();
	}

	protected void pointerReleased(int x, int y) {
		// TODO Auto-generated method stub
		super.pointerReleased(x, y);
		if (isIn) {
			play.actionUp(x, y);
			help.actionUp(x, y);
			about.actionUp(x, y);
			exit.actionUp(x, y);
		}
		repaint();
	}

	public boolean inSprite(Sprite s, int x, int y) {
		return x > s.getX() && x < s.getX() + s.getWidth() && y > s.getY()
				&& y < s.getY() + s.getHeight();
	}

}
